//
//  OEMViewController.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-18.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "OEMViewController.h"
#import "Imports1.h"

@interface OEMViewController(Private)
- (void) add1fingerTap;
- (void) add1fingerLeftSwipe;
- (void) testTapGestureRecognizer;
- (void) closeOEM;
@end

@implementation OEMViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UIImageView * imageV = (UIImageView *)[self.view viewWithTag:TagOfUIImageView];
        imageV.image = [FileManager getImage:@"oem.png" type:2 withRect:imageV.frame];
        [self performSelector:@selector(checkCloseOEM) withObject:nil afterDelay:5];
        [self add1fingerTap];
        [self add1fingerLeftSwipe];
    }
    return self;
}
- (void) checkCloseOEM{
    MyNSLog(@"in checkCloseOEM");
    if (self.view.superview) {
        MyNSLog(@"oemview's superview is not nil!");
        [self closeOEM];
    }else{
        MyNSLog(@"oemview's superview is nill");
    }
}

#pragma mark - GestureRecognizer
- (void) add1fingerTap{//手指单击
	UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
    recognizer.numberOfTouchesRequired = 1;//多少根手指
    recognizer.numberOfTapsRequired = 1;//点击几次
    recognizer.delegate = self;
	[self.view addGestureRecognizer:recognizer];
	[recognizer release];	
}
- (void) handleTapFrom:(UITapGestureRecognizer *)recognizer {
    MyNSLog(@"in handleTapFrom");
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未完成...");
        return;
    }
    AppSession.isAnimating = YES;
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    [self closeOEM];
}


- (void) add1fingerLeftSwipe//往左滑动
{
	UISwipeGestureRecognizer *recognizer1;
	recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
	recognizer1.direction = UISwipeGestureRecognizerDirectionLeft;
	[self.view addGestureRecognizer:recognizer1];
	[recognizer1 release];
	
}
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
    MyNSLog(@"in handleSwipeLeft");
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未完成...");
        return;
    }
    AppSession.isAnimating = YES;
    [AppSession playMusic:VoiceFile(@"trash.caf")];
    [self closeOEM];
}
- (void) closeOEM{
    MyNSLog(@"MenuBookChoose will be added");
    NSDictionary *infos = [NSDictionary dictionaryWithObjectsAndKeys:@"OEMViewController", @"FromNibName", @"MenuBookChoose",@"ToNibName",nil];    
    postNWithInfos(SysMsg_RootView_ControllerChange, self, infos);
}

#pragma mark - 测试TapGestureRecognizer
- (void) testTapGestureRecognizer{
    //一个手指点击一次
    UITapGestureRecognizer *singleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];  
    singleTapOne.numberOfTouchesRequired = 1; singleTapOne.numberOfTapsRequired = 1; singleTapOne.delegate = self;  
    //一个手指点击两次
    UITapGestureRecognizer *singleTapTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];  
    singleTapTwo.numberOfTouchesRequired = 2; singleTapTwo.numberOfTapsRequired = 1; singleTapTwo.delegate = self;  
    //两个手指点击一次
    UITapGestureRecognizer *doubleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];  
    doubleTapOne.numberOfTouchesRequired = 1; doubleTapOne.numberOfTapsRequired = 2; doubleTapOne.delegate = self;  
    //两个手指点击两次
    UITapGestureRecognizer *doubleTapTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];  
    doubleTapTwo.numberOfTouchesRequired = 2; doubleTapTwo.numberOfTapsRequired = 2; doubleTapTwo.delegate = self;  
    
    [singleTapOne requireGestureRecognizerToFail:doubleTapOne]; // 双击时不能被单击拦截 当两个手势有冲突的时候，忽略前面一个
    [singleTapTwo requireGestureRecognizerToFail:doubleTapTwo];  
    
    [self.view addGestureRecognizer:singleTapOne]; [singleTapOne release];  
    [self.view addGestureRecognizer:singleTapTwo]; [singleTapTwo release];  
    [self.view addGestureRecognizer:doubleTapOne]; [doubleTapOne release];  
    [self.view addGestureRecognizer:doubleTapTwo]; [doubleTapTwo release];  
}
- (void) handleSingleTap:(UITapGestureRecognizer *)sender {  
    if (sender.numberOfTouchesRequired == 1) {  
        NSLog(@"Single Tap with a finger.");  
    }  
    else if (sender.numberOfTouchesRequired == 2) {  
        NSLog(@"Single Tap with two finger.");  
    }  
}  
- (void) handleDoubleTap:(UITapGestureRecognizer *)sender {  
    if (sender.numberOfTouchesRequired == 1) {  
        NSLog(@"Double Tap with a finger.");  
    }  
    else if (sender.numberOfTouchesRequired == 2) {  
        NSLog(@"Double Tap with two finger.");  
    }  
}  
#pragma mark - View lifecycle
- (void)viewDidLoad{
    [super viewDidLoad];
}

- (void)viewDidUnload{
    [super viewDidUnload];
}
-(void) dealloc{
    MyNSLog(@"OEMViewController is deallocing");
    [super dealloc];
}
- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
	return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

@end
